package org.nmgyjt.springboot.controller;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.nmgyjt.springboot.beans.excel.TypeDataListener;
import org.nmgyjt.springboot.beans.vo.PeopleExcel;
import org.nmgyjt.springboot.entity.ApiRes;
import org.nmgyjt.springboot.entity.equipage.Equipage;
import org.nmgyjt.springboot.entity.rescueTeam.RescueTeam;
import org.nmgyjt.springboot.service.ICsSystemUserService;
import org.nmgyjt.springboot.service.RescueTeamService;
import org.nmgyjt.springboot.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;

@RestController
@RequestMapping("/cs/rescueTeam")
@Slf4j
public class RescueTeamController {

    @Autowired
    private RescueTeamService rescueTeamService;
    private TypeService typeService;
    @Autowired
    private ICsSystemUserService userService;
    @Autowired
    private CsSystemUserController csSystemUserController;

    @GetMapping("/getByDepId")
    public ApiRes getByDepId(String depId,Integer current,Integer size,String equipageName) {
        current=current==null?1:current;
        size=size==null?1000:size;
        Page<Equipage> page=new Page<>(current,size);
        return ApiRes.ok(rescueTeamService.getByDepId(depId,page,equipageName));
    }

    @GetMapping("/deleteByDepId")
    public ApiRes deleteByDepId(String depId) {
        Boolean b=rescueTeamService.deleteByDepId(depId);
        if(b){
            return ApiRes.ok();
        }else{
            return ApiRes.customFail("删除失败");
        }

    }

    @GetMapping("/getRescueTeamInfo")
    public ApiRes getRescueTeamInfo(String id,Integer current,Integer size) {
        current=current==null?1:current;
        size=size==null?1000:size;
        Page<Equipage> page=new Page<>(current,size);
        return ApiRes.ok(rescueTeamService.getRescueTeamInfo(id,page));
    }

//    @GetMapping("/getById")
//    public ApiRes getById(Long id) {
//        return ApiRes.ok(rescueTeamService.findById(id));
//    }

    @PostMapping("/saveOrUpdate")
    public ApiRes saveOrUpdate(@RequestBody RescueTeam rescueTeam) {
        return rescueTeamService.insertOrUpdate(rescueTeam);
    }

    @GetMapping("/list")
    public ApiRes list(String depId) {
        return ApiRes.ok(rescueTeamService.list());
    }

    @GetMapping("/testSendSms")
    public ApiRes testSendSms() {
        return rescueTeamService.testSendSms();
    }

    @PostMapping("/upload")
    @ResponseBody
    public String upload(@RequestParam("file") MultipartFile file, String createdBy)  {
        try {
            log.info("<<<<<<<<<<<<<<<<<<<<<<<<<<createBy:{}",createdBy);
            EasyExcel.read(file.getInputStream(), PeopleExcel.class, new TypeDataListener(userService,csSystemUserController,createdBy)).sheet().doRead();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:manage/sorttypeList";
    }
    @GetMapping("/download")
    @ResponseBody
    public void upload(HttpServletResponse response)  {
        try {
            // path是指想要下载的文件的路径
            File file = new File("/usr/local/cs_info_maintence/people_temp.xlsx");
            log.info(file.getPath());
            // 获取文件名
            String filename = file.getName();
            // 获取文件后缀名
            String ext = filename.substring(filename.lastIndexOf(".") + 1).toLowerCase();
            log.info("文件后缀名：" + ext);

            // 将文件写入输入流
            FileInputStream fileInputStream = new FileInputStream(file);
            InputStream fis = new BufferedInputStream(fileInputStream);
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            fis.close();

            // 清空response
            response.reset();
            // 设置response的Header
            response.setCharacterEncoding("UTF-8");
            //Content-Disposition的作用：告知浏览器以何种方式显示响应返回的文件，用浏览器打开还是以附件的形式下载到本地保存
            //attachment表示以附件方式下载   inline表示在线打开   "Content-Disposition: inline; filename=文件名.mp3"
            // filename表示文件的默认名称，因为网络传输只支持URL编码的相关支付，因此需要将文件名URL编码后进行传输,前端收到后需要反编码才能获取到真正的名称
            response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
            // 告知浏览器文件的大小
            response.addHeader("Content-Length", "" + file.length());
            OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/octet-stream");
            outputStream.write(buffer);
            outputStream.flush();
        } catch (IOException ex) {
            ex.printStackTrace();
        }

    }

}
